/**
 * @file displayhelper.h
 * @brief 
 *
 * 
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_DISPLAY_DISPLAYHELPER_H_
#define MYBOOK_DISPLAY_DISPLAYHELPER_H_

class CreatePageData
{
public:
	DWORD dwSize;
	LPTSTR szUrl;

	static CreatePageData* Init(LPCTSTR lpszUrl)
	{
		ATLASSERT(lpszUrl);
		if (lpszUrl == NULL)
			return NULL;

		DWORD dwLength = (_tcslen(lpszUrl) + 1) * sizeof(TCHAR);	
		CreatePageData* pData = (CreatePageData*)CGlobalAllocator::Allocate(sizeof(DWORD) + dwLength);

		pData->dwSize = sizeof(DWORD) + dwLength;
		memcpy((LPBYTE)pData + sizeof(DWORD), lpszUrl, dwLength);

		return pData;
	}

	static void Release(CreatePageData* pData)
	{
		if (pData != NULL)
		{
			CGlobalAllocator::Free(pData);
		}
	}

	static HWND CreateNewPage(HWND hMainWnd, LPCTSTR lpszURL)
	{
		CreatePageData *pData = CreatePageData::Init(lpszURL);
		return SendMessageCreatePage(hMainWnd, pData);
	}

	static HWND SendMessageCreatePage(HWND hMainWnd, CreatePageData* pData)
	{
		// cross processes
		ATLASSERT(pData && pData->dwSize);

		HWND hChildFrame = NULL;
		COPYDATASTRUCT cds = { (ULONG_PTR)hMainWnd, pData->dwSize, pData };

		//DWORD dwResult = 0;
		//SendMessageTimeout(hMainWnd, WM_COPYDATA, 0, (LPARAM)&cds, SMTO_BLOCK | SMTO_NOTIMEOUTIFNOTHUNG, 0, &dwResult);
		//return (HWND)dwResult;

		return (HWND)SendMessage(hMainWnd, WM_COPYDATA, 0, (LPARAM)&cds);
	}
};

#endif // MYBOOK_DISPLAY_DISPLAYHELPER_H_